import asyncio

from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.middleware import async_mysql_middleware as db
from com.arcfox.school.spider.universityimgs.university_url_spider import UniversityUrlSpider
from com.arcfox.util.redis_key_manager import UNIVERSITY_URLS_KEY


class UniversityImgService:
    def __init__(self):
        self.redis_manager = RedisTaskManager(UNIVERSITY_URLS_KEY)
        self.university_img_spider = UniversityUrlSpider()

    async def add_tasks(self):
        exist_task_size = await self.redis_manager.task_size()
        if exist_task_size > 0:
            return
        mapping = db.MysqlMapping("tb_university_info")
        rows = await mapping.query({}, ['university_uid', 'university_name', 'university_url'],
                                   [{"row": "university_rank", "asc": True}])
        await self.redis_manager.add_tasks(list(map(lambda r: {"uid": r[0], "name": r[1], "url": r[2]}, rows)))

    async def start(self):
        await self.add_tasks()
        tasks = [
            asyncio.create_task(self.university_img_spider.start_crawl()),
        ]
        await asyncio.gather(*tasks)


if __name__ == "__main__":
    asyncio.run(UniversityImgService().start())
